﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using GdHotel.Data;
using GdHotel.Data.Interfaces;
using GdHotel.Data.Meta.Models;
using GdHotel.Data.Templates;
using GdHotel.Web.Attributes;
using UniqueTechnologies.Attributes;
using UniqueTechnologies.Data.Interfaces;

namespace GdHotel.Web.Controllers
{

    [LayoutSupport]
    [LanguageSupport]
    public class ReservationController : BaseUiController
    {
        private const String CommonKey = "common";

        public override ILanguagesRepository LanguagesRepository { get; set; }
        public override INotesRepository NotesRepository { get; set; }
        public override IMainMenuItemsRepository MainMenuItemsRepository { get; set; }
        public override IAdditionalMenuItemsRepository AdditionalMenuItemsRepository { get; set; }
        public IEmailsRepository EmailsRepository { get; set; }
        public IMessagesRepository MessagesRepository { get; set; }
        public IGalleriesRepository GalleriesRepository { get; set; }

        public ReservationController(
            ILanguagesRepository languagesRepository, 
            INotesRepository notesRepository, 
            IMainMenuItemsRepository mainMenuItemsRepository, 
            IAdditionalMenuItemsRepository additionalMenuItemsRepository,
            IEmailsRepository emailsRepository,
            IMessagesRepository messagesRepository,
            IGalleriesRepository galleriesRepository)
        {
            LanguagesRepository = languagesRepository;
            NotesRepository = notesRepository;
            MainMenuItemsRepository = mainMenuItemsRepository;
            AdditionalMenuItemsRepository = additionalMenuItemsRepository;
            EmailsRepository = emailsRepository;
            MessagesRepository = messagesRepository;
            GalleriesRepository = galleriesRepository;
        }

        //
        // GET: /Reservation/

        public ActionResult Index()
        {
            return RedirectToAction("ReserveRoom");
        }

        [HttpGet]
        public ActionResult ReserveRoom()
        {
            var gallery = GalleriesRepository.FindAllByKey(CommonKey).FirstOrDefault();
            ViewData["Gallery"] = gallery;
            return View();
        }

        //public ActionResult ReserveHall()
        //{
        //    return View();
        //}

        [HttpPost]
        public ActionResult ReserveRoomConfirmed(ReserveRoomModel model)
        {
            var text = GenerateText(model, Templates.ReserveRoom);
            String recipients = String.Join(", ", EmailsRepository.FindAll().Select(e => e.Address).ToArray());
            var message = new Message {Text = text, Title = "Бронирование комнаты", Recipients = recipients};

            MessagesRepository.Insert(message);
            MessagesRepository.Save();
            
            return RedirectToAction("Success");
        }

        [HttpPost]
        public ActionResult ReserveHallConfirmed(ReserveHallModel model)
        {
            var text = GenerateText(model, Templates.ReserveHall);
            String recipients = String.Join(", ", EmailsRepository.FindAll().Select(e => e.Address).ToArray());
            var message = new Message { Text = text, Title = "Бронирование конференц-зала", Recipients = recipients };

            MessagesRepository.Insert(message);
            MessagesRepository.Save();

            return RedirectToAction("Success");
        }

        public ActionResult Success()
        {
            return View();
        }

        /// <summary>
        /// Generates body of email-message from model.
        /// </summary>
        /// <param name="model">Model to use for replacements.</param>
        /// <param name="template">Template with [%xxxx%] replacement places.</param>
        /// <returns></returns>
        private static string GenerateText(Object model, String template)
        {
            var builder = new TemplateBuilder(template);
            var modelType = model.GetType();
            var modelProperties = modelType.GetProperties();
            foreach (var property in modelProperties)
            {
                var value = property.GetValue(model, null);
                builder.AppendReplacement(property.Name, value);
            }

            var text = builder.Build();
            return text;
        }
    }
}
